#include "include.h"

#define LPK_DI	0x29
#define SPK_DI	0x15

code u8	dcsrst[] = {0x05,0x01,0x00};
//*************************************
// Sleep-Out
//*************************************
code u8	Sleep_Out[] = {0x05,0x11,0x00};
code u8	Sleep_IN[] = {0x05,0x10,0x00};
//DELAY	120
// Display-On
//*************************************
code u8	Display_On[] = {0x05,0x29,0x00};

#define CMDNUM  (7)

code u8	dcs0[] = {0xb9,0xff,0x83,0x63};	
code u8	dcs1[] = {0xba,0x80,0x00,0x10,0x08,0x08,0x10,0x7e,0x6e,0x6d,0x0a,0x01,0x80,0x43};
code u8	dcs2[] = {0x36,0x00};
code u8	dcs3[] = {0x3A,0x70};
code u8	dcs4[] = {0xB1,0x78,0x24,0x04,0x02,0x02,0x03,0x10,0x10,0x34,0x3C,0x3F,0x3F};
code u8	dcs5[] = {0xB4,0x00,0x08,0x6E,0x07,0x01,0x01,0x62,0x01,0x57};
code u8	dcs6[] = {0xCC,0x0B};
code u8	dcs7[] = {0xE0,0x01,0x48,0x4D,0x4E,0x58,0xF6,0x0B,0x4E,0x12,
		  0xD5,0x15,0x95,0x55,0x8E,0x11,0x01,0x48,0x4D,0x55,0x5F,
	      0xFD,0x0A,0x4E,0x51,0xD3,0x17,0x95,0x96,0x4E,0x11};

code u8 *cmdlist[]={
dcs0,dcs1,dcs2,dcs3,dcs4,dcs5,dcs6,dcs7
//,dcs8,dcs9,dcs10,dcs11,dcs12,dcs13,dcs14,dcs15
};

code u8 cmdlen[]={
sizeof(dcs0),sizeof(dcs1),sizeof(dcs2),sizeof(dcs3),sizeof(dcs4),sizeof(dcs5),sizeof(dcs6),sizeof(dcs7)
//,sizeof(dcs8),sizeof(dcs9),sizeof(dcs10),sizeof(dcs11),sizeof(dcs12),sizeof(dcs13),sizeof(dcs14),sizeof(dcs15)
};



void DcsPktWrite(u8 DCS_DI, u8 Len, u8* Ptr )
{
	u8 i=0;
	HDMI_WriteI2C_Byte( 0xff, 0xd4 );
	if( Len == 2 )
	{  	
		HDMI_WriteI2C_Byte( 0x01, 0x0c );
		HDMI_WriteI2C_Byte( 0x02, 0x04 );
		HDMI_WriteI2C_Byte( 0x03, DCS_DI );
		HDMI_WriteI2C_Byte( 0x03, *Ptr );
		HDMI_WriteI2C_Byte( 0x03, *( Ptr + 1 ) );
	}
	else
	{
		HDMI_WriteI2C_Byte( 0x01, 0x0E );
		HDMI_WriteI2C_Byte( 0x02, Len + 6 );
		HDMI_WriteI2C_Byte( 0x03, DCS_DI );
		HDMI_WriteI2C_Byte( 0x03, Len );
		HDMI_WriteI2C_Byte( 0x03, 0x00 );
		
		for( i = 0; i < Len; i++ )
		{
			HDMI_WriteI2C_Byte( 0x03, *Ptr );
			Ptr++;														   
		}
	}
	Timer0_Delay1ms(1);
	HDMI_WriteI2C_Byte( 0x01, 0x00 );
}

void Generic_Short_Write_1P(u8 data0 , u8 data1)
{
	HDMI_WriteI2C_Byte( 0xff, 0xd4 );
	HDMI_WriteI2C_Byte( 0x01, 0x0c );
	HDMI_WriteI2C_Byte( 0x02, 0x04 );
	HDMI_WriteI2C_Byte( 0x03, 0x15 );
	HDMI_WriteI2C_Byte( 0x03, data0 );
	HDMI_WriteI2C_Byte( 0x03, data1 );
	Timer0_Delay1ms(1);
	HDMI_WriteI2C_Byte( 0x01, 0x00 );
}

void DCS_Short_Write_NP(u8 data0)
{
	HDMI_WriteI2C_Byte( 0xff, 0xd4 );
	HDMI_WriteI2C_Byte( 0x01, 0x0c );
	HDMI_WriteI2C_Byte( 0x02, 0x04 );
	HDMI_WriteI2C_Byte( 0x03, 0x05 );
	HDMI_WriteI2C_Byte( 0x03, data0 );
	HDMI_WriteI2C_Byte( 0x03, 0x00 );
	Timer0_Delay1ms(1);
	HDMI_WriteI2C_Byte( 0x01, 0x00 );
}

void LCD_Reset(void)
{
   P10 = 0;
   Timer0_Delay1ms(200);
   P10 = 1;
   Timer0_Delay1ms(200);
}

void InitPanel( void )
{
	u8 dcslen=0,i=0;
	
	HDMI_WriteI2C_Byte(0xff,0x81);
	HDMI_WriteI2C_Byte(0x0e,0xef);
	HDMI_WriteI2C_Byte(0x0e,0xff);
	HDMI_WriteI2C_Byte(0x0b,0xfe);

	HDMI_WriteI2C_Byte(0xff,0x86);
	HDMI_WriteI2C_Byte(0x47,0x01);
	HDMI_WriteI2C_Byte(0x48,0x01);

	HDMI_WriteI2C_Byte(0xff,0xd4);
	HDMI_WriteI2C_Byte(0x20,0x2f);
	HDMI_WriteI2C_Byte(0x21,0x10);
	
	LCD_Reset();
	printf("\r\nStart initial panel");
  Generic_Short_Write_1P(0xE0,0x00);	
  Generic_Short_Write_1P(0xE1,0x93);	
  Generic_Short_Write_1P(0xE2,0x65);	
  Generic_Short_Write_1P(0xE3,0xF8);
  Generic_Short_Write_1P(0x80,0x01);		
  Generic_Short_Write_1P(0xE0,0x01);	
  Generic_Short_Write_1P(0x00,0x00);	
  Generic_Short_Write_1P(0x01,0xB6);	
  Generic_Short_Write_1P(0x03,0x00);	
  Generic_Short_Write_1P(0x04,0xC5);	
  Generic_Short_Write_1P(0x17,0x00);	
  Generic_Short_Write_1P(0x18,0xBF);	
  Generic_Short_Write_1P(0x19,0x01);	
  Generic_Short_Write_1P(0x1A,0x00);	
  Generic_Short_Write_1P(0x1B,0xBF);  
  Generic_Short_Write_1P(0x1C,0x01);	
  Generic_Short_Write_1P(0x1F,0x7C);	
  Generic_Short_Write_1P(0x20,0x26);	
  Generic_Short_Write_1P(0x21,0x26);	
  Generic_Short_Write_1P(0x22,0x4E);	
  Generic_Short_Write_1P(0x37,0x09);	
  Generic_Short_Write_1P(0x38,0x04);	
  Generic_Short_Write_1P(0x39,0x08);	
  Generic_Short_Write_1P(0x3A,0x1F);	
  Generic_Short_Write_1P(0x3B,0x1F);	
  Generic_Short_Write_1P(0x3C,0x78);	
  Generic_Short_Write_1P(0x3D,0xFF);	
  Generic_Short_Write_1P(0x3E,0xFF);	
  Generic_Short_Write_1P(0x3F,0x00);	
  Generic_Short_Write_1P(0x40,0x04);	
  Generic_Short_Write_1P(0x41,0xA0);	
  Generic_Short_Write_1P(0x43,0x0F);	
  Generic_Short_Write_1P(0x44,0x0A);	
  Generic_Short_Write_1P(0x45,0x24);		
  Generic_Short_Write_1P(0x55,0x01);	
  Generic_Short_Write_1P(0x56,0x01);	
  Generic_Short_Write_1P(0x57,0xA5);	
  Generic_Short_Write_1P(0x58,0x0A);	
  Generic_Short_Write_1P(0x59,0x4A);	
  Generic_Short_Write_1P(0x5A,0x38);	
  Generic_Short_Write_1P(0x5B,0x10);	
  Generic_Short_Write_1P(0x5C,0x19);	
  Generic_Short_Write_1P(0x5D,0x7C);
  Generic_Short_Write_1P(0x5E,0x61);
  Generic_Short_Write_1P(0x5F,0x51);
  Generic_Short_Write_1P(0x60,0x44);
  Generic_Short_Write_1P(0x61,0x3F);
  Generic_Short_Write_1P(0x62,0x2F);
  Generic_Short_Write_1P(0x63,0x33);
  Generic_Short_Write_1P(0x64,0x1C);
  Generic_Short_Write_1P(0x65,0x33);
  Generic_Short_Write_1P(0x66,0x31);
  Generic_Short_Write_1P(0x67,0x30);
  Generic_Short_Write_1P(0x68,0x58);
  Generic_Short_Write_1P(0x69,0x38);
  Generic_Short_Write_1P(0x6A,0x42);
  Generic_Short_Write_1P(0x6B,0x2F);
  Generic_Short_Write_1P(0x6C,0x23);
  Generic_Short_Write_1P(0x6D,0x13);
  Generic_Short_Write_1P(0x6E,0x02);
  Generic_Short_Write_1P(0x6F,0x08);
  Generic_Short_Write_1P(0x70,0x7C);
  Generic_Short_Write_1P(0x71,0x61);
  Generic_Short_Write_1P(0x72,0x51);
  Generic_Short_Write_1P(0x73,0x44);
  Generic_Short_Write_1P(0x74,0x3F);
  Generic_Short_Write_1P(0x75,0x2F);
  Generic_Short_Write_1P(0x76,0x33);
  Generic_Short_Write_1P(0x77,0x1C);
  Generic_Short_Write_1P(0x78,0x33);
  Generic_Short_Write_1P(0x79,0x31);
  Generic_Short_Write_1P(0x7A,0x30);
  Generic_Short_Write_1P(0x7B,0x37);
  Generic_Short_Write_1P(0x7C,0x38);
  Generic_Short_Write_1P(0x7D,0x42);
  Generic_Short_Write_1P(0x7E,0x2F);
  Generic_Short_Write_1P(0x7F,0x23);
  Generic_Short_Write_1P(0x80,0x13);
  Generic_Short_Write_1P(0x81,0x02);
  Generic_Short_Write_1P(0x82,0x08);
  Generic_Short_Write_1P(0xE0,0x02);	
  Generic_Short_Write_1P(0x00,0x57);	
  Generic_Short_Write_1P(0x01,0x77);	
  Generic_Short_Write_1P(0x02,0x44);	
  Generic_Short_Write_1P(0x03,0x46);	
  Generic_Short_Write_1P(0x04,0x48);	
  Generic_Short_Write_1P(0x05,0x4A);	
  Generic_Short_Write_1P(0x06,0x4C);	
  Generic_Short_Write_1P(0x07,0x4E);	
  Generic_Short_Write_1P(0x08,0x50);	
  Generic_Short_Write_1P(0x09,0x55);	
  Generic_Short_Write_1P(0x0A,0x52);	
  Generic_Short_Write_1P(0x0B,0x55);	
  Generic_Short_Write_1P(0x0C,0x55);	
  Generic_Short_Write_1P(0x0D,0x55);	
  Generic_Short_Write_1P(0x0E,0x55);	
  Generic_Short_Write_1P(0x0F,0x55);	
  Generic_Short_Write_1P(0x10,0x55);	
  Generic_Short_Write_1P(0x11,0x55);	
  Generic_Short_Write_1P(0x12,0x55);	
  Generic_Short_Write_1P(0x13,0x40);	
  Generic_Short_Write_1P(0x14,0x55);	
  Generic_Short_Write_1P(0x15,0x55);	
  Generic_Short_Write_1P(0x16,0x57);	
  Generic_Short_Write_1P(0x17,0x77);	
  Generic_Short_Write_1P(0x18,0x45);	
  Generic_Short_Write_1P(0x19,0x47);	
  Generic_Short_Write_1P(0x1A,0x49);	
  Generic_Short_Write_1P(0x1B,0x4B);	
  Generic_Short_Write_1P(0x1C,0x4D);	
  Generic_Short_Write_1P(0x1D,0x4F);	
  Generic_Short_Write_1P(0x1E,0x51);	
  Generic_Short_Write_1P(0x1F,0x55);	
  Generic_Short_Write_1P(0x20,0x53);	
  Generic_Short_Write_1P(0x21,0x55);	
  Generic_Short_Write_1P(0x22,0x55);	
  Generic_Short_Write_1P(0x23,0x55);	
  Generic_Short_Write_1P(0x24,0x55);	
  Generic_Short_Write_1P(0x25,0x55);	
  Generic_Short_Write_1P(0x26,0x55);	
  Generic_Short_Write_1P(0x27,0x55);	
  Generic_Short_Write_1P(0x28,0x55);	
  Generic_Short_Write_1P(0x29,0x41);	
  Generic_Short_Write_1P(0x2A,0x55);	
  Generic_Short_Write_1P(0x2B,0x55);	
  Generic_Short_Write_1P(0x2C,0x57);	
  Generic_Short_Write_1P(0x2D,0x77);	
  Generic_Short_Write_1P(0x2E,0x4F);	
  Generic_Short_Write_1P(0x2F,0x4D);	
  Generic_Short_Write_1P(0x30,0x4B);	
  Generic_Short_Write_1P(0x31,0x49);	
  Generic_Short_Write_1P(0x32,0x47);	
  Generic_Short_Write_1P(0x33,0x45);	
  Generic_Short_Write_1P(0x34,0x41);	
  Generic_Short_Write_1P(0x35,0x55);	
  Generic_Short_Write_1P(0x36,0x53);	
  Generic_Short_Write_1P(0x37,0x55);	
  Generic_Short_Write_1P(0x38,0x55);	
  Generic_Short_Write_1P(0x39,0x55);	
  Generic_Short_Write_1P(0x3A,0x55);	
  Generic_Short_Write_1P(0x3B,0x55);	
  Generic_Short_Write_1P(0x3C,0x55);	
  Generic_Short_Write_1P(0x3D,0x55);	
  Generic_Short_Write_1P(0x3E,0x55);	
  Generic_Short_Write_1P(0x3F,0x51);	
  Generic_Short_Write_1P(0x40,0x55);	
  Generic_Short_Write_1P(0x41,0x55);	
  Generic_Short_Write_1P(0x42,0x57);	
  Generic_Short_Write_1P(0x43,0x77);	
  Generic_Short_Write_1P(0x44,0x4E);	
  Generic_Short_Write_1P(0x45,0x4C);	
  Generic_Short_Write_1P(0x46,0x4A);	
  Generic_Short_Write_1P(0x47,0x48);	
  Generic_Short_Write_1P(0x48,0x46);	
  Generic_Short_Write_1P(0x49,0x44);	
  Generic_Short_Write_1P(0x4A,0x40);	
  Generic_Short_Write_1P(0x4B,0x55);	
  Generic_Short_Write_1P(0x4C,0x52);	
  Generic_Short_Write_1P(0x4D,0x55);	
  Generic_Short_Write_1P(0x4E,0x55);	
  Generic_Short_Write_1P(0x4F,0x55);	
  Generic_Short_Write_1P(0x50,0x55);	
  Generic_Short_Write_1P(0x51,0x55);	
  Generic_Short_Write_1P(0x52,0x55);	
  Generic_Short_Write_1P(0x53,0x55);	
  Generic_Short_Write_1P(0x54,0x55);	
  Generic_Short_Write_1P(0x55,0x50);	
  Generic_Short_Write_1P(0x56,0x55);	
  Generic_Short_Write_1P(0x57,0x55); 	
  Generic_Short_Write_1P(0x58,0x40); 	
  Generic_Short_Write_1P(0x59,0x00); 	
  Generic_Short_Write_1P(0x5A,0x00); 	
  Generic_Short_Write_1P(0x5B,0x10); 	
  Generic_Short_Write_1P(0x5C,0x09); 	
  Generic_Short_Write_1P(0x5D,0x30); 	
  Generic_Short_Write_1P(0x5E,0x01); 	
  Generic_Short_Write_1P(0x5F,0x02); 	
  Generic_Short_Write_1P(0x60,0x30); 	
  Generic_Short_Write_1P(0x61,0x03); 	
  Generic_Short_Write_1P(0x62,0x04); 	
  Generic_Short_Write_1P(0x63,0x06);	
  Generic_Short_Write_1P(0x64,0x6A); 	
  Generic_Short_Write_1P(0x65,0x75); 	
  Generic_Short_Write_1P(0x66,0x0F); 	
  Generic_Short_Write_1P(0x67,0xB3); 	
  Generic_Short_Write_1P(0x68,0x0B); 	
  Generic_Short_Write_1P(0x69,0x06);
  Generic_Short_Write_1P(0x6A,0x6A); 	
  Generic_Short_Write_1P(0x6B,0x10); 	
  Generic_Short_Write_1P(0x6C,0x00); 	
  Generic_Short_Write_1P(0x6D,0x04); 	
  Generic_Short_Write_1P(0x6E,0x04); 	
  Generic_Short_Write_1P(0x6F,0x88); 	
  Generic_Short_Write_1P(0x70,0x00); 	
  Generic_Short_Write_1P(0x71,0x00); 	
  Generic_Short_Write_1P(0x72,0x06); 	
  Generic_Short_Write_1P(0x73,0x7B); 	
  Generic_Short_Write_1P(0x74,0x00); 	
  Generic_Short_Write_1P(0x75,0xBC); 	
  Generic_Short_Write_1P(0x76,0x00); 	
  Generic_Short_Write_1P(0x77,0x05); 	
  Generic_Short_Write_1P(0x78,0x2E); 	
  Generic_Short_Write_1P(0x79,0x00); 	
  Generic_Short_Write_1P(0x7A,0x00); 	
  Generic_Short_Write_1P(0x7B,0x00); 	
  Generic_Short_Write_1P(0x7C,0x00); 	
  Generic_Short_Write_1P(0x7D,0x03); 	
  Generic_Short_Write_1P(0x7E,0x7B);
  Generic_Short_Write_1P(0xE0,0x04);	
  Generic_Short_Write_1P(0x09,0x10);
  Generic_Short_Write_1P(0x2B,0x2B);	
  Generic_Short_Write_1P(0x2E,0x44);	
  Generic_Short_Write_1P(0xE0,0x00);	
  Generic_Short_Write_1P(0xE6,0x02);	
  Generic_Short_Write_1P(0xE7,0x02);	
  Generic_Short_Write_1P(0x35,0x00);
	
	DcsPktWrite( Sleep_Out[0], ( sizeof( Sleep_Out ) - 1 ), &Sleep_Out[1] );
	Timer0_Delay1ms(120);	
	DcsPktWrite( Display_On[0], ( sizeof( Display_On ) - 1 ), &Display_On[1] );
	Timer0_Delay1ms(20);
	printf("\r\nFinish initial panel");
} 

